package com.nexgo.payment.trans.setting.resetgps;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.download.DownloadTransActivity;
import com.nexgo.payment.trans.setting.password.SetupPasswordActivity;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * GPS重置
 *
 * @author 谢豪景
 * @time 2018年8月22日
 */
public class ResetGPSActivity extends BaseActivity implements BaseActivity.DownTimerListener {
    private static final String TAG = SetupPasswordActivity.class.getSimpleName();
    private static final int REQCODE = 123;

    @BindView(R.id.tv_setup_resetgps_ckv)
    EditText setupResetgpsCkv;
    @BindView(R.id.tv_setup_resetgps_random)
    EditText setupResetgpsRandom;
    @BindView(R.id.tv_setup_resetgps_merchant_no)
    EditText setupResetgpsMerchantNo;
    @BindView(R.id.tv_setup_resetgps_terminal_no)
    EditText setupResetgpsTerminalNo;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;
    @BindView(R.id.title_tv_right)
    TextView tvTitleRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_resetgps);
        ButterKnife.bind(this);
        initTitle();
        String merchantid = Repository.getInstance().getParamValue(PreferencesConstants.MERCHANT_ID);
        String terminalid = Repository.getInstance().getParamValue(PreferencesConstants.TERMINAL_ID);

        setupResetgpsMerchantNo.setText(merchantid);
        setupResetgpsTerminalNo.setText(terminalid);
        setupResetgpsRandom.setText(setRandom());
        //启动计时器
        setDownTimer(SysConstant.TIME_OUT_60*3, this);
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.reset_gps))
                .build();
    }

    @OnClick({R.id.title_iv_left, R.id.btn_confirm})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()){
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.btn_confirm:
                String ResetgpsCkv = setupResetgpsCkv.getText().toString();
                if(ResetgpsCkv.length()<8){
                    ToastUtils.showToast("校验值长度为8位");
                    return;
                }
                if(!TextUtils.isEmpty(ResetgpsCkv)){
                    Repository.getInstance().setParamValue(PreferencesConstants.RESETGPSCKV,ResetgpsCkv);
                    startActivityForResult(DownloadTransActivity.newInstance(this, TransType.RESET_GPS),REQCODE);
                    finish();
                }else{
                    ToastUtils.showToast("校验值不能为空");
                    return;
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        finish();
    }

    public String setRandom(){
        StringBuilder str=new StringBuilder();//定义变长字符串
        Random random=new Random();
        //随机生成数字，并添加到字符串
        for(int i=0;i<8;i++){
            str.append(random.nextInt(10));
        }
        Repository.getInstance().setParamValue(PreferencesConstants.RESETGPSRANDOM,str.toString());
        return str.toString();
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, ResetGPSActivity.class);
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        tvTitleRight.setText("0");
        finish();
    }
}
